#include <Servo.h>

Servo servo1;


void setup() {

  pinMode(1,OUTPUT);
  servo1.attach(14); //analog pin 0
  pinMode(13, INPUT);     
      servo1.write(0);

}

int spindle_on = 0;

void loop() {
  int input_count =0;
  for(int i = 0; i<100; i++)
  {
    if(digitalRead(13) != spindle_on)input_count++;
    delay(1);
  }
  
  if(input_count == 100)
  {
    if(spindle_on == 0)spindle_on = 1;
    else spindle_on = 0;
    
    if(spindle_on == 0)
    {
      // we have just been asked to turn off the spindle
    for(int i = 0; i<100; i++)
    {
      servo1.write(180);
      delay(20);
    }
    for(int i = 0; i<100; i++)
    {
      servo1.write(0);
      delay(20);
    }
    }
  }
} 
